package com.wwj.my_ummary.util.httputils;

import okhttp3.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public class Okhttp3Utils {


    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    /**
     * post 请求 表单提交
     */
    public static String sendPostForm(String url, Map<String, String> param, Map<String, String> headerMap) {
        OkHttpClient client = new OkHttpClient();
        FormBody.Builder builder = new FormBody.Builder();

        if (param != null) {
            if (!param.isEmpty()) { //如果param里面没有数据返回true
                Iterator<Map.Entry<String, String>> it = param.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, String> next = it.next();
                    builder.add(next.getKey(), next.getValue());
                }
            }
        }
        Request.Builder post = new Request.Builder()
                .url(url)
                .post(builder.build());
        if (headerMap != null) {
            if (!headerMap.isEmpty()) {
                Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
                while (dd.hasNext()) {
                    Map.Entry<String, String> next = dd.next();
                    post.addHeader(next.getKey(), next.getValue());
                }
            }
        }

        //同步请求
        Call call = client.newCall(post.build());
        try {
            Response response = call.execute();
            return response.body().string();
        } catch (IOException e) {
            return null;
        }

    }

    /**
     * post 发送json
     */
    public static String sendPostJson(String url, String json, Map<String, String> headerMap) {
        OkHttpClient client = new OkHttpClient();

        RequestBody body = RequestBody.create(JSON, json);

        Request.Builder post = new Request.Builder()
                .url(url)
                .post(body);
        if (headerMap != null) {
            if (!headerMap.isEmpty()) {
                Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
                while (dd.hasNext()) {
                    Map.Entry<String, String> next = dd.next();
                    post.addHeader(next.getKey(), next.getValue());
                }
            }
        }
        //同步请求
        Call call = client.newCall(post.build());
        try {
            Response response = call.execute();
            return response.body().string();
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * get 请求
     */
    public static String sendGet(String url, Map<String, Object> param, Map<String, String> headerMap) {
        OkHttpClient client = new OkHttpClient();

        StringBuffer stringBuffer = new StringBuffer();

        if (param != null) {
            if (!param.isEmpty()) {
                Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
                while (it.hasNext()) {
                    Map.Entry<String, Object> next = it.next();
                    if ("".equals(stringBuffer.toString())) {
                        stringBuffer.append(next.getKey() + "=" + next.getValue());
                    } else {
                        stringBuffer.append("&" + next.getKey() + "=" + next.getValue());
                    }
                }
            }
        }
        if (!"".equals(stringBuffer)) {
            url = url + "?";
            url = url + stringBuffer.toString();
        }
        Request.Builder get = new Request.Builder()
                .url(url);
        if (headerMap != null) {
            if (!headerMap.isEmpty()) {
                Iterator<Map.Entry<String, String>> dd = headerMap.entrySet().iterator();
                while (dd.hasNext()) {
                    Map.Entry<String, String> next = dd.next();
                    get.addHeader(next.getKey(), next.getValue());
                }
            }
        }
        //同步请求
        Call call = client.newCall(get.build());
        try {
            Response response = call.execute();
            return response.body().string();
        } catch (IOException e) {
            return null;
        }
    }
}
